/*********************************************************************
*
*	 File: obj.h
*	 Declare methods use for Build Tree and Check Collision 
*	 Author: 10520418 Bui Huu Hiep
*
**********************************************************************/
#pragma once
#ifndef __METHOD_H
#define __METHOD_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <vector>
#include "QNode.h"
#include "Mario.h"
#include "Land.h"
#include "Brick.h"
#include "Mushroom.h"
#include "Star.h"
#include "Coin.h"
#include "QBrick.h"
#include "Goomba.h"
#include "EatFlower.h"
#include "Koopa.h"
#include "Pipe.h"
#include "Scene.h"
#include "Resources.h"
#include "Viewport.h"
#include "Ladder.h"
#include "Boss.h"

#include <stdlib.h>

using namespace std;

// Direction collison
enum DirCollision
{
	NONE,COLLISION,
	LEFT, RIGHT, TOP, BOTTOM
};

//Declare methods use for Build Tree and Check Collision
class Method
{
private:
	// Split string by delimiter
	static vector<int> Split(string line, string delimiter);
	
	// Get objects in Viewport
	static void Clip(RECT viewport, QNode* rootNode, vector<GameObject*> &listObjects);
	
	// Read all info map from File
	static void ReadInfoFromFile(string path, vector<string> &stringObjects, vector<string> &stringUpdate, vector<string> &stringRender);
	
	// Create Objects and add it into std::map
	static map<int, GameObject*> AllObjects(vector<string> &list, Resources* &res, Mario* &mario);
	
	// Create Nodes and add it into std::map
	static map<double, QNode*> AllNode(vector<string> &list, map<int, GameObject*> &listObjects);
	
	// Build Tree All Node
	static void BuildTree(map<double, QNode*> &listNode, QNode* &rootNode);

	static RECT RectBoundCurrentAndOldRect(GameObject* object);

	// Get All Objects
	static void GetAllObject(vector<GameObject*> &list, QNode* &Tree);

	// Save Tree
	static void GetAllNode(vector<QNode*> &listNode, QNode* &Tree);

public:
	// Get all object in Viewport
	static void GetObjectInViewport(RECT rect, QNode* rootNode, vector<GameObject*> &list, vector<GameObject*> &listStatic);
	
	// Build Tree from All Objects and All Node
	static void BuildTree(string path, Resources* res, QNode* &TreeUpdate, QNode* &TreeRender, Mario* &mario);
	
	// Save  Tree Update and Tree Render into file
	// Use Save Game
	static void SaveTree(string path, QNode* &TreeUpdate, QNode* &TreeRender, Mario* mario);
	
	// Check collision between 2 Rectangle
	static bool RectToRect(RECT A, RECT B);

	static DirCollision IsCollision(GameObject* A, GameObject* B, float &colTime);

	// Sort when process collision
	static bool MinDistance(GameObject* A, GameObject* B);

	// Sort when render
	static bool MaxDepth(GameObject* A, GameObject* B);
};
#endif